﻿@{
    ViewBag.Title = "GroupCompany";
}

<input id="gid" value="@ViewBag.GID" class="mini-hidden" />
<div style="width: 100%; height: 100%">
    <div class="mini-toolbar" style="border-top: 0; border-left: 0; border-right: 0;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 20%">
                    <a class="mini-button" plain="true" iconcls="icon-save" onclick="save()">保存</a>
                </td>
                <td style="width: 80%; text-align: right">
                    <input id="txt" class="mini-textbox" width="200px" onenter="search()" />
                    <a class="mini-button" plain="true" iconcls="icon-search" onclick="search()">搜索</a>
                </td>
            </tr>
        </table>

    </div>
    <div class="mini-fit">
        <div id="treegrid" class="mini-treegrid" style="width: 100%; height: 100%;" treecolumn="CName"
            idfield="CID" parentfield="FID" resultastree="false" showcheckbox="true" checkrecursive="false"
            autocheckparent="true" checkedfield="IsChecked" allowresize="false" expandonload="true" onnodecheck="onNodeCheck">
            <div property="columns">
                <div type="indexcolumn"></div>
                <div name="CName" field="CName" width="200px">名称</div>
                <div field="CNote" width="100%">备注</div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();

    var treegrid = mini.get("treegrid");
    var url = "/SystemData/Group/GetAllCompany?gid=" + mini.get("gid").getValue();
    treegrid.setUrl(url);

    function save() {
        var modified = treegrid.getCheckedNodes();
        var jsonEdit = mini.encode(modified)//把数组转换为JSON
        jsonEdit = escape(jsonEdit);//编码

        treegrid.loading("正在保存");

        $.ajax({
            url: "/SystemData/Group/SaveGroupCompanyData",
            type: "post",
            data: { edit: jsonEdit, gid: mini.get("gid").getValue() },
            success: function (text, flag, res) {
                treegrid.unmask();
                if (res.responseText == "") {
                    top.mini.showTips({ content: "保存成功", state: "info", x: "center", y: "center", timeout: 1000 });
                } else {
                    $.tip({ text: "保存失败", staytime: 1000 });
                }
            },
            error: function (text, flag, res) {
                $.tip({ text: "保存失败", staytime: 1000 });
            }
        });
    }

    function search() {
        var key = mini.get("txt").getValue();
        if (key == "") {
            treegrid.clearFilter();
        } else {
            key = key.toLowerCase();
            treegrid.filter(function (node) {
                var text = node.CName ? node.CName.toLowerCase() : "";
                if (text.indexOf(key) != -1) {
                    return true;
                }
            });
        }
    }

    function onNodeCheck(e) {
        var node = e.node; var nodes = treegrid.getAllChildNodes(node);
        if (node.checked) {
            treegrid.checkNodes(nodes);
        }
        else {
            treegrid.uncheckNodes(nodes);
        }
    }
</script>
